$(function () {

    getUserInfo();
    $("#registeredButton").click(function () {

        $.ajax({
            headers: {
                "Content-Type": "application/json; charset=utf-8"
            },
            url: "/api/register",
            data: JSON.stringify({
                "phone": $("#phone2").val(),
                "password": $("#password2").val(),
                "name": $("#username2").val(),
                "phoneVerificationCode": $("#phoneVerificationCode").val(),
            }),
            type: "post",
            dataType: "json",
            success: function (data, status) {
                alert(data.msg);
                if(data.code!==0&&data.code!=='0'){
                    console.log(data.msg);
                }else {
                    $('#registeredModal').modal('hide')
                }
            }
        });
    });
    $("#forgetPasswordButton").click(function () {

        $.ajax({
            headers: {
                "Content-Type": "application/json; charset=utf-8"
            },
            url: "/api/forgetPassword",
            data: JSON.stringify({
                "phone": $("#phone3").val(),
                "password": $("#password3").val(),
                "phoneVerificationCode": $("#phoneVerificationCode1").val(),
            }),
            type: "post",
            dataType: "json",
            success: function (data, status) {
                alert(data.msg);
                if(data.code!==0&&data.code!=='0'){
                    console.log(data.msg);
                }else {
                    $('#forgetPassword').modal('hide')
                }
            }
        });
    });
    $("#loginButton").click(function () {

        $.ajax({
            headers: {
                "Content-Type": "application/json; charset=utf-8"
            },
            url: "/api/login",
            data: JSON.stringify({
                "phone": $("#phone").val(),
                "password": $("#password").val(),
            }),
            type: "post",
            dataType: "json",
            success: function (data, status) {
                alert(data.msg);
                $.cookie('token', data.data.token);
                getUserInfo();
            }
        });
    });
})


function getUserInfo() {

    let token = $.cookie('token');
    if (token === undefined || token === "") {
        return undefined;
    } else {
        $.ajax({
            headers: {
                Accept: "application/json; charset=utf-8",
                token: token
            },
            url: "/api/userInfo",
            type: "get",
            success: function (data, status) {
                //alert("数据: " + data + "\n状态: " + status);
                let userInfo = data.data;
                if (userInfo) {
                    $("#registered").hide();
                    $("#login").hide();
                    $("#userInfo").show();
                    $("#userName").text(userInfo.name)
                } else {
                    $("#login").show();
                }
                return data;
            }
        });
    }
}
